from flask import Flask, render_template, request, escape
from sign_in_up import sign_in, sign_up
from functions import getinfo, geocode, planning
import os

app = Flask(__name__)


@app.route('/signing_in', methods=['POST'])
def sign_in_page() -> 'html':
    username = request.form['username']
    password = request.form['password']
    sign_in(username, password)
    title = 'log日志'
    titles = ['起点和终点', '访问方式', '访问地址']
    data = []
    with open('history.log', encoding="gbk") as file:
        for i in file:
            data.append([])
            for j in i.split(" | "):
                data[-1].append(escape(j))
    if sign_in(username, password) == 'yes':
        return render_template('log.html',
                               the_title=title,
                               table_titles=titles,
                               the_data=data)
    else:
        return render_template('again.html')


@app.route('/signing_up', methods=['POST'])
def sign_up_page() -> 'html':
    username = request.form['username']
    password = request.form['password']
    sign_up(username, password)
    return render_template('sign_in.html')


@app.route('/')
def index_page() -> 'html':
    return render_template('index.html')


@app.route('/functions')
def functions_page() -> 'html':
    return render_template('functions.html')


@app.route('/to_log')
def to_log_page() -> 'html':
    return render_template('to_log.html')


@app.route('/experience')
def experience_page() -> 'html':
    return render_template('experience.html')


@app.route('/sign_in')
def sign_i_page() -> 'html':
    return render_template('sign_in.html')


@app.route('/sign_up')
def sign_u_page() -> 'html':
    return render_template('sign_up.html')


@app.route('/log')
def log_page() -> 'html':
    title = 'log日志'
    titles = ['起点和终点', '访问方式', '访问地址']
    data = []
    with open('history.log', encoding="gbk") as file:
        for i in file:
            data.append([])
            for j in i.split(" | "):
                data[-1].append(escape(j))
    return render_template('log.html',
                           the_title=title,
                           table_titles=titles,
                           the_data=data)


@app.route('/process', methods=['POST'])
# 数据来源
def do_search() -> 'html':
    departure1 = request.form['Departure']
    destination1 = request.form['Destination']
    title = '结果:'
    departure2 = geocode(key='2d015ae1da99945790f1601af060c108', address=departure1)
    destination2 = geocode(key='2d015ae1da99945790f1601af060c108', address=destination1)
    results = planning(departure2, destination2)
    results = getinfo(results)
    with open('to_speak.txt', 'a') as f:
        print(results, file=f)
    os.system('python tts.py')
    with open('history.log', 'a') as f:
        print(request.form, request.user_agent, request.remote_addr, file=f, sep=" | ")
    return render_template('result.html',
                           the_title=title,
                           the_results=results,
                           the_departure=departure1,
                           the_destiantion=destination1)

@app.route('/code')
def code_page() -> 'html':
    return render_template('code.html')


if __name__ == '__main__':
    app.run()
